package com.ewancle.controller

import com.ewancle.entity.Fruit
import com.ewancle.service.UserService
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
import io.micronaut.serde.annotation.SerdeImport
import io.micronaut.serde.annotation.Serdeable

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Controller("/users")
class UserController(private val userService: UserService) {

    // http://localhost:5055/users get
    @Get("/")
    suspend fun getAllUsers(): List<Fruit> = withContext(Dispatchers.IO) {
        userService.getAllUsers()
    }

    // http://localhost:5055/users post {"id":6,"name":"赵六"}
    @Post("/")
    fun createUser(@Body request: CreateUserRequest): Int {
        return userService.createUser(request.id, request.name)
    }

    @Serdeable
    data class CreateUserRequest(val id: Long, val name: String)
}